package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.SysArea;
import com.example.demo.mapper.AreaMapper;
import com.example.demo.utils.JDBCUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/area")
public class AreaController {

    @Resource
    private AreaMapper areaMapper;

//    @GetMapping("/tree")
//    public Result<?> findAll() {
//        List<Area> areas = areaMapper.selectList(null);
//        // 第一级
//        List<Area> pNodeList = areas.stream().filter(area -> area.getPid() == null).collect(Collectors.toList());
//        for (Area area : pNodeList) {
//            Integer pid = area.getId();
//            // 第二级
//            List<Area> secondNodeList = areas.stream().filter(a -> pid.equals(a.getPid())).collect(Collectors.toList());
//            area.setChildren(secondNodeList);
//
//            // 第三级
//            for (Area secondArea : secondNodeList) {
//                Integer secondPid = secondArea.getId();
//                List<Area> thirdNodeList = areas.stream().filter(a -> secondPid.equals(a.getPid())).collect(Collectors.toList());
//                secondArea.setChildren(thirdNodeList);
//            }
//        }
//        return Result.success(pNodeList);
//    }
@PostMapping
public Result<?> save(@RequestBody SysArea SysArea) {
    areaMapper.insert(SysArea);
    return Result.success();
}

    @PutMapping
    public Result<?> update(@RequestBody SysArea SysArea) {
        areaMapper.updateById(SysArea);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        areaMapper.deleteById(id);
        return Result.success();
    }

//    @PostMapping("/synchronise")
//    public Result<?> synchronise(){
//        JDBCUtil.getArea();
//        return Result.success();
//    }

//    @PostMapping("/deleteBatch")
//    public Result<?> deleteBatch(@RequestBody List<Integer> ids) {
//        activityMapper.deleteBatchIds(ids);
//        return Result.success();
//    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Integer id) {
        return Result.success(areaMapper.selectById(id));
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String name) {
        LambdaQueryWrapper<SysArea> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(name)) {
            wrapper.like(SysArea::getName, name);
        }
        Page<SysArea> areaPage = areaMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(areaPage);
    }
    @GetMapping("/all")
    public Result<?> findAll() {
        return Result.success(areaMapper.selectList(null));
    }


}
